package com.shqcjd.generalplatformprojectclient.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.model.pojo.GuideListBean;
import com.shqcjd.generalplatformprojectclient.model.pojo.InviteBean;
import com.shqcjd.generalplatformprojectclient.model.pojo.RemoteGuidanceBean;
import com.shqcjd.generalplatformprojectclient.ui.activity.EquipmentInfoActivity;
import com.shqcjd.generalplatformprojectclient.ui.activity.GuidanceHistoryActivity;
import com.shqcjd.generalplatformprojectclient.util.DateUtil;

import java.util.List;

public class CeshiAdapter extends BaseQuickAdapter<GuideListBean.ContentBean,BaseViewHolder> {
    static final int VIEW_TYPE_GUIDE = R.layout.activity_main_item_guide;
    static final int VIEW_TYPE_STICKY = R.layout.activity_main_item_title;
    static final int VIEW_TYPE_HISTORY = R.layout.activity_main_item_history;
    static final int VIEW_TYPE_NONE = R.layout.activity_main_item_none;
    private List<GuideListBean.ContentBean> data;
    private Context context;
    public boolean isGuideIng = false;//是否显示进行中
    private RemoteGuidanceBean remoteGuidanceBean = new RemoteGuidanceBean();

    private StickyAdapter.setOnGuideIngClickListener listener;

    public void setOnGuideIngClickListener(StickyAdapter.setOnGuideIngClickListener listener){
        this.listener = listener;
    }

    public interface setOnGuideIngClickListener{
        void onReceive();
        void onRefuse();
    }

    public CeshiAdapter(@Nullable List<GuideListBean.ContentBean> data, Context context) {
        super(R.layout.item_invite_layout, data);
        this.data = data;
        this.context = context;
    }

    public void setGuideIng(RemoteGuidanceBean remoteGuidanceBean){
        this.remoteGuidanceBean = remoteGuidanceBean;
    }

    @Override
    public int getItemCount() {
        return isGuideIng?data.size()+3:data.size()+1;
    }

    public List<GuideListBean.ContentBean> getmList(){
        return data;
    }

    @Override
    protected int getDefItemViewType(int position) {
        if ((data.size()!=0&&position==0)||(data.size()!=0&&position==2&&isGuideIng)||(position==0&&isGuideIng)) {
            return VIEW_TYPE_STICKY;
        }else if(position==1&&isGuideIng){
            return VIEW_TYPE_GUIDE;
        }else if(data.size()==0){
            return VIEW_TYPE_NONE;
        }
        return VIEW_TYPE_HISTORY;
    }

    @Override
    protected void convert(BaseViewHolder helper, GuideListBean.ContentBean item) {
        if (helper instanceof TitleViewHolder){
            TitleViewHolder h = (TitleViewHolder) helper;
            if(isGuideIng&&helper.getAdapterPosition()==0){
                h.text.setText("进行中");
            }else{
                h.text.setText("远程指导记录");
            }
        }else if(helper instanceof GuideViewHolder){
            GuideViewHolder h = (GuideViewHolder) helper;
            h.tv_item_main_guide_phone.setText(remoteGuidanceBean.inviterPhoneNumber+"发起的远程指导");
            h.tv_item_main_guide_title.setText(remoteGuidanceBean.equipmentName);
            h.tv_item_main_guide_type.setText(remoteGuidanceBean.tag.equals("1")?"设备":"设备类型");
            h.btn_item_main_guide_refuse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.onRefuse();
                    }
                }
            });

            h.btn_item_main_guide_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.onReceive();
                    }
                }
            });
        }else if(helper instanceof HistoryViewHolder){
            int p = isGuideIng?helper.getAdapterPosition()-3:helper.getAdapterPosition()-1;
            HistoryViewHolder h = (HistoryViewHolder) helper;
            h.tv_item_main_history_phone.setText(item.inviteePhoneNumber+"发起的远程指导");
            h.tv_item_main_history_title.setText(item.equipmentName);
            h.tv_item_main_history_type.setText(item.tag==1?"设备":"设备类型");
            h.tv_item_main_history_date.setText(DateUtil.getDateToString(item.invitationTime,"yyyy-MM-dd HH:mm:ss"));
            h.tv_item_main_history_info.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent it = new Intent(context, GuidanceHistoryActivity.class);
                    it.putExtra("eId", item.id);
                    context.startActivity(it);
                }
            });
            h.rl_item_main_history.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent it = new Intent(context, EquipmentInfoActivity.class);
                    it.putExtra("eId", item.id);
                    it.putExtra("type",item.tag);
                    context.startActivity(it);
                }
            });
        }else if(helper instanceof NoneHolder){

        }
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if(viewType==VIEW_TYPE_STICKY){
            return new TitleViewHolder(LayoutInflater.from(context).inflate(R.layout.activity_main_item_title, parent, false));
        }else if(viewType==VIEW_TYPE_HISTORY){
            return new HistoryViewHolder(LayoutInflater.from(context).inflate(R.layout.activity_main_item_history, parent, false));
        }else if(viewType==VIEW_TYPE_NONE){
            return new NoneHolder(LayoutInflater.from(context).inflate(R.layout.activity_main_item_none, parent, false));
        }
        return new GuideViewHolder(LayoutInflater.from(context).inflate(R.layout.activity_main_item_guide,parent, false));

    }

    private class TitleViewHolder extends BaseViewHolder  {

        private TextView text;

        TitleViewHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.tv_title);
        }
    }


    private class GuideViewHolder extends BaseViewHolder {
        private TextView tv_item_main_guide_phone, tv_item_main_guide_title, tv_item_main_guide_type;
        private Button btn_item_main_guide_confirm, btn_item_main_guide_refuse;

        GuideViewHolder(View itemView) {
            super(itemView);
            tv_item_main_guide_phone = itemView.findViewById(R.id.tv_item_main_guide_phone);
            tv_item_main_guide_title = itemView.findViewById(R.id.tv_item_main_guide_title);
            tv_item_main_guide_type = itemView.findViewById(R.id.tv_item_main_guide_type);
            btn_item_main_guide_confirm = itemView.findViewById(R.id.btn_item_main_guide_confirm);
            btn_item_main_guide_refuse = itemView.findViewById(R.id.btn_item_main_guide_refuse);
        }
    }

    private class HistoryViewHolder extends BaseViewHolder  {
        private TextView tv_item_main_history_phone, tv_item_main_history_title, tv_item_main_history_type, tv_item_main_history_date, tv_item_main_history_info;
        private RelativeLayout rl_item_main_history;
        HistoryViewHolder(View itemView) {
            super(itemView);
            tv_item_main_history_phone = itemView.findViewById(R.id.tv_item_main_history_phone);
            tv_item_main_history_title = itemView.findViewById(R.id.tv_item_main_history_title);
            tv_item_main_history_type = itemView.findViewById(R.id.tv_item_main_history_type);
            tv_item_main_history_date = itemView.findViewById(R.id.tv_item_main_history_date);
            tv_item_main_history_info = itemView.findViewById(R.id.tv_item_main_history_info);
            rl_item_main_history = itemView.findViewById(R.id.rl_item_main_history);
            Log.e("tou", "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+itemView.getId());
        }

    }

    private class NoneHolder extends BaseViewHolder  {


        NoneHolder(View itemView) {
            super(itemView);
        }
    }

}
